package cn.jiangjiesheng.bootstrap.integration.datapool.params;

import com.google.common.collect.Lists;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Data
@Accessors(chain = true)
@NoArgsConstructor
public class ReqActionParam implements Serializable {

    private ReqActionEnum reqAction;

    private ReadActionParam readActionParam;


    private WriteActionParam writeActionParam;


    @Data
    @Accessors(chain = true)
    @NoArgsConstructor
    public static class ReadActionParam implements Serializable {
        /**
         * 读取一条
         */
        private Long id;
        /**
         * 用于幂等处理
         */
        private String requestId;
        private String entId;
        private String tableId;
        /**
         * 默认为IdAsc
         */
        private Integer offset = 0;
        private Integer limit = 20;
        /**
         * 要返回的字段
         */
        private List<String> columns = Lists.newArrayList();

        public ReadActionParam addColumn(String column) {
            this.columns.add(column);
            return this;
        }

        public String getSelectClause() {
            if (this.columns.isEmpty())
                return "*";
            else {
                return this.columns.stream().reduce((a, b) -> a + "," + b).get();
            }
        }
    }

    @Data
    @Accessors(chain = true)
    @NoArgsConstructor
    public static class WriteActionParam implements Serializable {
        /**
         * 用于幂等处理
         */
        private String requestId;
        private String entId;
        private String tableId;
        /**
         * 用于查重，组合字段使用逗号分隔
         */
        private String uniqueField;
        private String appId;
        private Map<String, Object> record;
    }

}
